package com.example.progressrecord

import android.annotation.SuppressLint
import android.app.DatePickerDialog
import android.content.DialogInterface
import android.os.Bundle
import android.widget.DatePicker
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import com.example.progressrecord.adpter.ProgressInfoAdapter
import com.example.progressrecord.databinding.ActivityMainBinding
import com.example.progressrecord.leancloud.ProgressInfo
import com.example.progressrecord.leancloud.ProgressInfoParser
import com.example.progressrecord.leancloud.ProgressInfoParser.toProgressInfo
import com.example.progressrecord.leancloud.ProgressInfoRepository
import jp.wasabeef.recyclerview.adapters.SlideInBottomAnimationAdapter
import jp.wasabeef.recyclerview.animators.SlideInDownAnimator
import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private val progressInfoAdapter = ProgressInfoAdapter()

    @SuppressLint("NotifyDataSetChanged")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.listRv.itemAnimator = SlideInLeftAnimator()

        binding.listRv.addItemDecoration(
            DividerItemDecoration(this, DividerItemDecoration.VERTICAL))

        binding.listRv.adapter = progressInfoAdapter

        lifecycleScope.launchWhenCreated {

            val list = ProgressInfoRepository
                .queryByTimeDesc()
                .map {
                    it.toProgressInfo()
                }
            progressInfoAdapter.setDataList(list)
            progressInfoAdapter.notifyDataSetChanged()

        }

        val addProgressInfoFragment = AddProgressInfoFragment()
        addProgressInfoFragment.onDismiss = {
            val savedInfo: ProgressInfo? =
                addProgressInfoFragment.arguments?.getParcelable(AddProgressInfoFragment.ADDED_INFO)
            savedInfo?.let {
                progressInfoAdapter.addDataHead(it)
            }
        }
        binding.floatingActionButton.setOnClickListener {
            addProgressInfoFragment.show(supportFragmentManager, "")
        }
    }
}